﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Celeriq.Common
{
	[Serializable()]
	[DataContract()]
	[KnownType(typeof(GeoCode))]
	[KnownType(typeof(GeoCodeFieldFilter))]
	public class DimensionItem : System.ICloneable
	{
		public DimensionItem()
		{
			this.RefinementList = new List<RefinementItem>();
		}

		[DataMember]
		public string Name;

		[DataMember]
		public int DIdx;

		[DataMember]
		public long? NumericBreak;

		[DataMember]
		public List<RefinementItem> RefinementList;

		[DataMember]
		public DimensionItem Parent;

		public long GetNextDVIdx()
		{
			if (this.RefinementList.Count == 0) return ((this.DIdx - DimensionDefinition.DGROUP) + 1) * DimensionDefinition.DVALUEGROUP;
			else return this.RefinementList.Max(x => x.DVIdx) + 1;
		}

		public override string ToString()
		{
			return this.Name;
		}

		object ICloneable.Clone()
		{
			var retval = new DimensionItem();
			retval.DIdx = this.DIdx;
			retval.Name = this.Name;
			retval.NumericBreak = this.NumericBreak;
			this.RefinementList.ForEach(x => retval.RefinementList.Add(((ICloneable) x).Clone() as RefinementItem));
			return retval;
		}

	}
}